package com.manage.webuser.controller;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manage.base.bean.CommonResultBean;
import com.manage.base.controller.BaseController;
import com.manage.util.StringUtil;
import com.manage.webuser.model.WebUser;
import com.manage.webuser.service.IWebUserService;

@Controller
@RequestMapping("/webuser")
public class WebUserController extends BaseController {

	private static final long serialVersionUID = 1L;

	static final Logger LOG = LoggerFactory.getLogger(WebUserController.class);

	@Autowired
	private IWebUserService webUserService;

	// 查询用户列表
	@RequestMapping(value = "select", method = RequestMethod.POST)
	@ResponseBody
	public CommonResultBean select(@RequestParam(value = "data", required = false) String data) {
		LOG.info("webuser select called query param: " + data);
		try {
			WebUser param = fromJson(data, WebUser.class);
			CommonResultBean crb = CommonResultBean.succBean();
			param = convert(param);
			List<WebUser> list = webUserService.select(param);
			int totalCount = webUserService.selectTotalCount(param);
			crb.put("size", totalCount);
			crb.put("webusers", list);
			return crb;
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			return CommonResultBean.errorBean("查询数据失败");
		}
	}

	private WebUser convert(WebUser param) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			if (StringUtil.notEmpty(param.registerDateStart)) {
				param.registerDateStartLong = new Timestamp(sdf.parse(param.registerDateStart).getTime());
			} else {
				param.registerDateStart = null;
			}

			if (StringUtil.notEmpty(param.registerDateEnd)) {
				param.registerDateEndLong = new Timestamp(sdf.parse(param.registerDateEnd).getTime());
			} else {
				param.registerDateEnd = null;
			}

		} catch (ParseException e) {
			LOG.error(e.getMessage(), e);
		}
		return param;
	}
}
